package com.chtw.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chtw.dao.AllmessageMapper;
import com.chtw.entity.Allmessage;
import com.chtw.entity.AllmessageExample;

@Service
public class AllMessageService {

	@Autowired
	private AllmessageMapper allMapper;

	public static String getTime() {
		Date date = new Date();
		SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
		String time = dateFormat.format(date);
		//System.out.println("当前时间:"+time);
		return time;
	}
	/**
	 * 添加消息
	 * @param allmessage
	 * @return
	 */
	public int addMessage(Allmessage allmessage,HttpSession session) {
		String time = getTime();
		allmessage.setSendtime(time);
		allmessage.setSendername((String)session.getAttribute("currentUser"));
		Integer accountLevel = (Integer)session.getAttribute("accountLevel");
		if(accountLevel==0) {
			allmessage.setStatu(0);
		}else {
			allmessage.setStatu(1);
		}
		allmessage.setMid(null);
		int i = allMapper.insertSelective(allmessage);
		return i;
	}
	
	
	/**
	 * 管理员获取待审核消息
	 * @return
	 */
	public List<Allmessage> getExamine() {
		AllmessageExample example = new AllmessageExample();
		AllmessageExample.Criteria cre = example.createCriteria();
		cre.andStatuEqualTo(1);
		List<Allmessage> aList = allMapper.selectByExample(example);
		return aList;
	}
	
	/**
	 * 管理员删除消息
	 * @param mid
	 * @return
	 */
	public int deleMessage(int mid) {
		int i = allMapper.deleteByPrimaryKey(mid);
		return i;
	}
	
	/**
	 * 审核操作(通过或不通过)
	 * @param mid
	 * @return
	 */
	public int Pass(int mid,int no) {
		AllmessageExample example = new AllmessageExample();
		AllmessageExample.Criteria cre = example.createCriteria();
		cre.andMidEqualTo(mid);
		Allmessage allmessage = new Allmessage();
		if(no==0) {
			allmessage.setStatu(0);//通过审核
		}else if(no==1) {
			allmessage.setStatu(-1);//未通过
		}else if(no==2) {
			allmessage.setStatu(-2);//忽略
		}else if(no==3) {
			allmessage.setStatu(1);//取消忽略-->待审核
		}
		int i = allMapper.updateByExampleSelective(allmessage, example);
		return i;
	}
	
	/**
	 * 获取审核通过的消息
	 * @return
	 */
	public List<Allmessage> getpass() {
		AllmessageExample example = new AllmessageExample();
		AllmessageExample.Criteria cre = example.createCriteria();
		cre.andStatuEqualTo(0);
		List<Allmessage> aList = allMapper.selectByExample(example);
		return aList;
	}
	
	/**
	 * 获取审核未通过的消息
	 * @return
	 */
	public List<Allmessage> getNopass() {
		AllmessageExample example = new AllmessageExample();
		AllmessageExample.Criteria cre = example.createCriteria();
		cre.andStatuEqualTo(-1);
		List<Allmessage> aList = allMapper.selectByExample(example);
		return aList;
	}
	
	/**
	 * 忽略掉的审核
	 * @return
	 */
	public List<Allmessage> gethulue() {
		AllmessageExample example = new AllmessageExample();
		AllmessageExample.Criteria cre = example.createCriteria();
		cre.andStatuEqualTo(-2);
		List<Allmessage> aList = allMapper.selectByExample(example);
		return aList;
	}
	
	/**
	 * 批量处理
	 * @return
	 */
	public int allfunc(String str) {
		AllmessageExample example = new AllmessageExample();
		AllmessageExample.Criteria cre = example.createCriteria();
		Allmessage allmessage = new Allmessage();
		if(str.equals("passall")) {
			cre.andStatuEqualTo(1);
			allmessage.setStatu(0);
			//通过所以审核
		}else if(str.equals("hulueall")) {
			cre.andStatuEqualTo(1);
			allmessage.setStatu(-2);
		}else if(str.equals("nohulueall")) {
			cre.andStatuEqualTo(-2);
			allmessage.setStatu(1);
		}else if(str.equals("deleall")) {
			cre.andStatuEqualTo(-1);
			return allMapper.deleteByExample(example);
		}
		return allMapper.updateByExampleSelective(allmessage, example);
		//return i;
	}
	
	
	/**
	 * 教师获取自己发布的消息
	 * @return
	 */
	public List<Allmessage> getmeadd(HttpSession session) {
		AllmessageExample example = new AllmessageExample();
		AllmessageExample.Criteria cre = example.createCriteria();
		cre.andSendernameEqualTo((String)session.getAttribute("currentUser"));
		return allMapper.selectByExample(example);
	}
	
	/**
	 * 学生获取低级审核通过的消息
	 * @return
	 */
	public List<Allmessage> getme() {
		AllmessageExample example = new AllmessageExample();
		AllmessageExample.Criteria cre = example.createCriteria();
		cre.andStatuEqualTo(0);
		cre.andMsgLevelEqualTo(1);
		return allMapper.selectByExample(example);
	}
	
	
}
